iconEuler Examples

Matlab Mode in Euler

These examples are from the web page "A very short introduction in Matlab", and from some other sources.

Let us switch to Matlab mode. Matlab uses a very short display by default.

>matlab on; shortestformat;

Matrices can now have blanks between elements. Note: For negative elements, use the comma, since [1 -1 1] would yield [0 1] in Euler.

>A=[16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
     16       3       2      13 
      5      10      11       8 
      9       6       7      12 
      4      15      14       1 

Matrix elements can be accessed with round brackets.

>A(1,1)+A(2,1)+A(3,1)+A(4,1)
34

The sum operator now works columns vectors differently.

>sum(A(1:4,1)) // use sum(A[1:4,1]') in Euler
34
>sum(A) // 
[34, 34, 34, 34]
>diag(A,0)
     16 
     10 
      7 
      1 

fliplr works like flipx, and flipud like flipy.

>sum(diag(fliplr(A)))
34

Another function which is available in EMT and Matlab is rot().

>rot(A,2)
      1      14      15       4 
     12       7       6       9 
      8      11      10       5 
     13       2       3      16 

Euler does also have an algorithm for magic triangles.

>magic(4)
     16       2       3      13 
      5      11      10       8 
      9       7       6      12 
      4      14      15       1 

Euler used the matrix language consistently for all operators. Matlab uses .* for the elementwise multiplication.

>v=1:10; v.*v
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

And it uses * for the matrix product.

>v*v'
385

The power operator now uses the matrix power function.

>A=[1,2;3,4]; A^2
      7      10 
     15      22 

This works for the inverse too.

>A^-1*A
      1       0 
      0       1 

The division uses "divide into" now. I.e.

b/A = (A'\b')'

>sum(A)/A
[1, 1]

The \ operator now uses fit.

>A=[1,2;3,4;5,6]; b=[1;2;3]; A\b
      0 
    0.5 

The same in Euler.

>fit(A,b)
      0 
    0.5 

In Matlab mode, you can use Matlab's syntax for functions. A proper "return y" statement will be added. If the function is the last or the only function in a file, "endfunction" can be omitted.

>function y=f(x)
 y=x^2;
 endfunction
>f(5)
25

Examples